package com.shopping.service.impl;

import com.shopping.bean.Comment;
import com.shopping.dao.CommentDao;
import com.shopping.service.ICommentService;
import com.shopping.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentService implements ICommentService {
    @Autowired
    CommentDao commentDao;

    @Override
    public Map<String, Object> listByProductId(int productid, int page, int limit, Integer type) {
        Page pageObj = new Page();
        pageObj.setPage(page);
        pageObj.setLimit(limit);
        int start = pageObj.getStart();
        List<Comment> comments = commentDao.listByProductId(productid, start, limit, type);
        int total = commentDao.countByProductId(productid, type);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", comments);
        result.put("total", total);
        return result;
    }

    @Override
    public boolean addComment(Comment comment) {
        int result = commentDao.add(comment);
        return result > 0;
    }

    @Override
    public List<Comment> listByUserId(int userid) {
        return commentDao.listByUserId(userid);
    }

    @Override
    public int deleteComment(Integer commentid) {
        return commentDao.deleteComment(commentid);
    }
}